package es.udc.fic.hamNeggs.web;

import es.udc.fic.hamNeggs.dao.InputProject;
import es.udc.fic.hamNeggs.dao.InputSprint;
import es.udc.fic.hamNeggs.dao.InputUser;
import es.udc.fic.hamNeggs.manager.ProjectManager;
import es.udc.fic.hamNeggs.manager.UserManager;
import es.udc.fic.hamNeggs.manager.WorksManager;
import es.udc.fic.hamNeggs.pojo.Project;
import es.udc.fic.hamNeggs.pojo.Sprint;
import es.udc.fic.hamNeggs.pojo.UserStories;
import es.udc.fic.hamNeggs.pojo.Works;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Servlet implementation class ProjectManagement
 */
public class ProjectManagement extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProjectManagement() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        final HttpSession session = request.getSession(true);
        final ProjectManager prjmng = new ProjectManager();
        final UserManager usrmng = new UserManager();

        final Long myUser = (Long) session.getAttribute("user");

        if (session.getAttribute("project") == null || (request.getParameter("change") != null && request.getParameter("change").equals("true"))) {
            if (request.getParameter("id") != null) {
                final Project project = prjmng.getProject(new Long(request.getParameter("id")));
                session.setAttribute("project", project.getId());
                session.setAttribute("projectName", project.getName());
                request.setAttribute("ustdone", project.getUserStoriesDone());

                final List<Works> userWorks = usrmng.getUser(myUser).getWorks();
                for (final Works work : userWorks)
                    if (work.getProject().getId().intValue() == project.getId().intValue())
                        session.setAttribute("rol", work.getRol().getString());
            } else {
                (new WelcomePage()).doGet(request, response);
                return;
            }
        }

        final Long myProject = (Long) session.getAttribute("project");

        final ArrayList<InputUser> iUsers = new ArrayList<InputUser>();
        for (final Works w : prjmng.getProject(myProject).getWorker()) {
            final InputUser user = new InputUser();
            user.setUser(w.getUser());
            user.setCurrentRol(w.getRol().getString());
            iUsers.add(user);
        }
        request.setAttribute("users", iUsers);

        final Project project = prjmng.getProject(myProject);
        final InputProject iProject = new InputProject();
        iProject.setProject(project);
        request.setAttribute("project", iProject);

        int spdone = 0, spestimated = 0;
        for (final UserStories ust : project.getUserStories()) {
            if (ust.getRealstoryPoints() != null) {
                spdone += ust.getRealstoryPoints();
            }
            if (ust.getEstimatedstoryPoints() != null)
                spestimated += ust.getEstimatedstoryPoints();
        }

        request.setAttribute("spdone", spdone);
        request.setAttribute("spestimated", spestimated);
        request.setAttribute("numerust", project.getUserStories().size());

        request.setAttribute("rol", session.getAttribute("rol"));

        final List<Sprint> sprints = project.getSprints();
        final ArrayList<InputSprint> iSprints = new ArrayList<InputSprint>();

        int totalsp = prjmng.getProject(myProject).getSPEstimated();

        for (final Sprint sprint : sprints) {
            final InputSprint iSprint = new InputSprint();
            iSprint.setSprint(sprint);
            totalsp -= iSprint.getStoryPointsDone();
            iSprint.setSpleft(totalsp);
            iSprints.add(iSprint);
        }

        request.setAttribute("sprints", iSprints);

        usrmng.close();
        prjmng.close();

        request.getRequestDispatcher("/projects.jsp").forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        final HttpSession session = request.getSession(true);

        final Long myProject = (Long) session.getAttribute("project");

        final WorksManager wrkmng = new WorksManager();

        Works wrk = wrkmng.getWorks(new Long(request.getParameter("user")), myProject);

        switch (request.getParameter("action")) {
            case "Delete":
                if (wrk != null) {
                    wrkmng.deleteWorks(wrk);
                }
                break;
            default:
                break;
        }
        wrkmng.close();

        doGet(request, response);
    }

}
